package com.csailing4cloud.csailing.redis.test.service;

import com.alibaba.fastjson.JSON;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.csailing4cloud.csailing.common.core.constants.CommonConstants;
import com.csailing4cloud.csailing.common.core.util.CsResponse;
import com.csailing4cloud.csailing.user.api.dto.UserInfo;
import com.csailing4cloud.csailing.user.api.entity.CsUser;
import com.csailing4cloud.csailing.user.api.feign.RemoteUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author dyh
 * 用户服务
 */
@AllArgsConstructor
@Service
public class UserService {

    private final RemoteUserService remoteUserService;

    public String getUserInfo(String userName){
        CsResponse<UserInfo> csUserCsResponse = remoteUserService.userInfo(userName);
        if(CommonConstants.FAIL.equals(csUserCsResponse.getCode())){
            return JSON.toJSONString(csUserCsResponse);
        }
        if(Objects.isNull(csUserCsResponse.getResult().getCsUser())){
            return null;
        }
        return JSON.toJSONString(csUserCsResponse.getResult().getCsUser());
    }

    @LcnTransaction
    public CsResponse saveUserInfo(String exFlag){
        CsUser csUser = new CsUser();
        csUser.setUserName("test");
        csUser.setPassword("123456");
        csUser.setCreateTime(LocalDateTime.now());
        csUser.setDelFlag(0);
        csUser.setLockFlag(0);
        csUser.setUpdateTime(LocalDateTime.now());
        CsResponse csResponse = remoteUserService.saveUserInfo(csUser);
        if(Objects.nonNull(exFlag)){
            throw new IllegalStateException("by exFlag");
        }
        return csResponse;
    }
}
